package com.monster.controller;

import com.monster.entity.Result;
import com.monster.service.OrderService;
import com.monster.utils.redis.RedisUtil;
import com.monster.utils.resources.RedisConstant;
import com.monster.utils.sms.ValidateCodeUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
public class OrderController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Reference
    OrderService orderService;

    @PostMapping("order/sendCode/{telephone}")
    public Result sendCode(@PathVariable String telephone){
        String code = ValidateCodeUtils.generateValidateCode(4);
        Map map =new HashMap<>();
        map.put("telephone",telephone);
        map.put("code",code);
        rabbitTemplate.convertAndSend("szby114",map);//health-monster
        RedisUtil.set(RedisConstant.ALL_SETMEAL_PIC_SET+telephone,code,90, TimeUnit.SECONDS);
        return new Result(code);
    }
    @PostMapping("order/sumbit")
    public Result sumbit(@RequestBody Map map){
        return  new Result(orderService.sumbit(map));
    }

    @GetMapping("/order/success/{id}")
    public Result successPage(@PathVariable("id") int id){
        Map map = orderService.showSuccessPage(id);
        return new Result(orderService.showSuccessPage(id));
    }
}
